package com.dykj.apps.web.sysmanager.dao;

import java.util.ArrayList;
import java.util.List;

import com.dykj.apps.web.sysmanager.dto.SysAuthDTO;
import com.dykj.platform.framework.common.paging.Hquery;
import com.dykj.platform.framework.common.paging.Page;
import com.dykj.platform.framework.common.paging.PagingHelper;
import com.dykj.platform.framework.exception.DatabaseException;
import com.dykj.platform.framework.common.paging.AbstractPQDAO;

public class AuthManagerDAOImpl extends AbstractPQDAO implements IAuthManagerDAO {

	public Hquery createHQ(Object objDTO, Page page) throws DatabaseException {
		SysAuthDTO dto = (SysAuthDTO)objDTO;
		Hquery hquery = new Hquery();
		List paramlist = new ArrayList();
		StringBuffer strBufHql = new StringBuffer();
		strBufHql.append("from SysAuth a where 1=1 ");
		//部门
		strBufHql.append(PagingHelper.createWherePara("and","a.authdesc","like",dto.getP_authdesc(),paramlist));
		strBufHql.append(PagingHelper.createWherePara("and", "a.creator", "=", dto.getCreator(), paramlist));
		hquery.setParalist(paramlist);
		hquery.setQueryString(strBufHql.toString());
		if(page.getSort()!=null){
			hquery.setSortOrder(page.getSort().getSortOrder());
			hquery.setOrderby(page.getSort().getProperty());
		}
		return hquery;
	}
	
	public List queryAllAuth(String authid) throws DatabaseException {
		String hql = "from SysAuth";
		return this.query(hql);
	}

	public List findGroupByAuthid(String authid) throws DatabaseException {
		 StringBuffer sb = new StringBuffer();										  //@@@groupid
		    sb.append(" select sysgroup from SysGroup sysgroup,SysAct sysact where sysgroup.org = sysact.groupid ");
		    sb.append(" and sysact.authid=");
		    sb.append("'"+Long.parseLong(authid)+"'");
		    sb.append(" order by sysgroup.groupid ");
		    String hql = sb.toString();	
		    return this.query(hql);
	}
	public List findAllGroup() throws DatabaseException {
		String sql="from SysGroup s ";				
		return this.query(sql);
	}
/*
 * 修改数据权限时，超级管理员查询nocheckgroup
 * (non-Javadoc)
 * @see com.dykj.apps.web.sysmanager.dao.IAuthManagerDAO#findSuperGroupNull(java.lang.String)
 */
	public List findSuperGroupNull(String authid) throws DatabaseException {
		String sql = "from SysGroup s where s.org not in ( select a.groupid from SysAct a where a.authid='"+Long.parseLong(authid)+"') order by s.groupid";
		return this.query(sql);			//@@@groupid
	}
/*
 * 修改数据权限时，普通管理员查询nocheckgroup
 * (non-Javadoc)
 * @see com.dykj.apps.web.sysmanager.dao.IAuthManagerDAO#findGroupNull(java.lang.String)
 */
	public List findGroupNull(String authid,String cauthid) throws DatabaseException {
										//@@@groupid
		String sql = "from SysGroup s where s.org in(select a.groupid from SysAct a where a.authid='" +
				Long.parseLong(cauthid)+"' and a.groupid not in(select act.groupid from SysAct act where act.authid='" +
						Long.parseLong(authid)+"'))order by s.groupid";
		return this.query(sql);
	}
	
}
